package com.example.springboottest.config;

import com.example.springboottest.student.model.Student;
import com.example.springboottest.student.rs.StuRSService;
import com.example.springboottest.student.rs.impl.StuRSServiceImpl;
import com.example.springboottest.student.ws.StuWSService;
import com.example.springboottest.student.ws.impl.StuWSServiceImpl;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class CxfConfig {
    @Autowired
    private StuWSService stuWSService;


    @Autowired
    private StuRSService stuRSService;

    /**
     * wsdl访问地址为：http://localhost:9090/fn/webservice/
     * wsdl访问地址为：http://localhost:9090/fn/webservice/stu?wsdl
     *
     * @return
     */
    @SuppressWarnings("all")
    @Bean
    public ServletRegistrationBean dispatcherCXFServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/fn/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint stuWSServiceEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), stuWSService);//绑定要发布的服务
        endpoint.publish("/stu"); //显示要发布的名称
        return endpoint;
    }

    @Bean
    public JAXRSServerFactoryBean stuRSServiceEndpoint() {
        // 添加ResourceClass
        List<Class<?>> resourceClassList = new ArrayList<Class<?>>();
        resourceClassList.add(StuRSServiceImpl.class);

        // 添加ResourceProvider
        List<ResourceProvider> resourceProviderList = new ArrayList<ResourceProvider>();
        resourceProviderList.add(new SingletonResourceProvider(
                stuRSService));

        //添加provider
        List<Object> providerList = new ArrayList<Object>();
        providerList.add(new JacksonJsonProvider());

        JAXRSServerFactoryBean jaxrsServerFactoryBean = new JAXRSServerFactoryBean();
//        jaxrsServerFactoryBean.setResourceClasses(resourceClassList);  //设置实体类
        jaxrsServerFactoryBean.setServiceBean(stuRSService);     //设置服务类
//        jaxrsServerFactoryBean.setResourceProviders(resourceProviderList);
//        jaxrsServerFactoryBean.setResourceClasses(StuRSServiceImpl.class);
//        jaxrsServerFactoryBean.setServiceBean(stuRSService);
        jaxrsServerFactoryBean.setAddress("http://localhost:9090/rs/webservice");  //设置地址
        jaxrsServerFactoryBean.setDocLocation("ssss");
//        jaxrsServerFactoryBean.setProviders(providerList);
        jaxrsServerFactoryBean.create();
        return jaxrsServerFactoryBean;
    }
}
